/**
 * Definition for a binary tree node.
 * function TreeNode(val, left, right) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.left = (left===undefined ? null : left)
 *     this.right = (right===undefined ? null : right)
 * }
 */
/**
 * @param {TreeNode} root
 * @return {number[]}
 */
var rightSideView = function(root) {
    if(root == null) return []
    let front = 0, rear = 0, levelPos = 0
    let queue = []
    let ans = []
    queue.push(root)
    rear++
    levelPos++
    while(front < rear) {
        let pnode = queue[front++]
        if(pnode.left) queue[rear++] = pnode.left
        if(pnode.right) queue[rear++] = pnode.right
        if(front === levelPos) {
            ans.push(pnode.val)
            levelPos = rear
        }
    }
    return ans
};